package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MusicAlbumAdapter;
import flc.ast.databinding.ActivityMusicAlbumAllBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;

public class MusicAlbumAllActivity extends BaseAc<ActivityMusicAlbumAllBinding> {

    private MusicAlbumAdapter musicAlbumAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_music_album_all;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivMusicAlbumAllBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivMusicAlbumAdd.setOnClickListener(this);
        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 2));
        musicAlbumAdapter = new MusicAlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(musicAlbumAdapter);
        musicAlbumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivMusicAlbumAdd://制作音乐相册
                startActivity(SelPictureActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path = musicAlbumAdapter.getItem(position);
        if (TextUtils.isEmpty(path)) {
            startActivity(SelPictureActivity.class);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        List<String> listPath = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myMusicAlbum;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            listPath.add("");
            for (File file : files) {
                listPath.add(file.getPath());
            }
        }
        if (listPath.size() > 0) {
            musicAlbumAdapter.setList(listPath);
            mDataBinding.rvAlbumList.setAdapter(musicAlbumAdapter);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llMusicAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llMusicAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

}